<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ] 
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/3/25-10:26
    // +----------------------------------------------------------------------
    // | Description:  部门岗位
    // +----------------------------------------------------------------------


    namespace app\admin\controller\company;


    use app\admin\controller\AdminBase;
    use app\admin\model\Stations;
    use app\admin\validate\StationValidate;
    use Jrk\Trees;

    class Station extends AdminBase
    {
        public function initialize()
        {
            parent::initialize(); // TODO: Change the autogenerated stub

            $this->model=app()->model("Stations");

            $this->del="岗位";
        }

        /**
         * @param Stations $stations
         * @return mixed
         * @throws \think\Exception
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/25
         * @name: addStation
         * @describe:添加
         */
        public function addStation(Stations $stations){
            if (IS_POST){
                $data = $this->request->post();
                $validate=new StationValidate();

                if (!$validate->check($data)){
                    $this->error($validate->getError());
                }

                if (isset($data['id'])){
                    addLog("岗位操作","修改岗位：".$data['sname']);
                }else{
                    $k=$stations->where("sname",$data['sname'])->where("d_id",$data['d_id'])->find();

                    if ($k){
                        $this->error("当前选择的部门下已有该岗位名称");
                    }

                    addLog("岗位操作","新增岗位：".$data['sname']);
                }
                return $this->model->addAndEdit($data);
            }


            $tree = Trees::toFormatTree(model("Departments")->deList(),"dname");//获取菜单列表

            $d_id = $this->request->param("id/d");//父id

            $this->assign(compact("d_id", "tree"));

            $this->assign("info",[]);

            return $this->fetch();
        }



        /**
         * @return mixed
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/25
         * @name: editStation
         * @describe:编辑
         */
        public function editStation(){

            $d_id = $this->request->param("id/d");//父id

            $info=$this->model->getOne($d_id);

            if (!$info){
                $this->assign('msg','当前id:'.$d_id.'错误');
                $this->fetch("public/404");
            }

            $tree = Trees::toFormatTree(model("Departments")->deList(),"dname");//获取菜单列表

            $this->assign(compact("d_id", "tree"));

            $this->assign("info",$info);

            return $this->fetch("add_station");
        }



    }